let ycmjtable = require('../../../domain/game/table');
let code = require('../../../consts/consts');
module.exports = function(app){
    return new Remote(app);
}
let Remote = function(app){
    this.app = app;
    this.roomMgr = this.app.get('roomMgr');
}
Remote.prototype.creatRoom = function(opts,cb){
    try {
        let table = new ycmjtable(this.app,opts);
        if(this.roomMgr.addTable(table)){
            if(!!cb && typeof cb === 'function'){
                cb(null,{
                    code:code.OK_CODE.CREATR_TABLE_SUCESS,
                    gameId:code.GAME_ID.YCMJ,
                    msg:{
                        tid:table.tid
                    }
                });
            }
        }else
        {
            if(!!cb && typeof cb === 'function'){
                cb({
                    code:code.FAIL,
                    gameId:code.GAME_ID.YCMJ,
                    msg:''
                });
            }
        };
    } catch (error) {
        if(!!cb && typeof cb === 'function'){
            cb({
                code:code.FAIL,
                gameId:code.GAME_ID.YCMJ,
                msg:code.FAIL_CODE.CREATR_TABLE_FAIL
            });
        }
    }
}
Remote.prototype.joinRoom = function(opts,cb){
    let table = this.roomMgr.getTable(opts.tid);
    if(!table){
        if(!!cb && typeof cb === 'function'){
            return cb({
                code:code.FAIL,
                gameId:code.GAME_ID.YCMJ,
                msg:error.code.FAIL_CODE.TBALE_NOT_EXIST
            });
        };
    };
    let res = table.joinTable(opts.player);
    cb(null,res);
}
Remote.prototype.gameExit = function(msg,next){
    let suid = msg.uid;
    let stid = msg.tid;
    if(!suid || !stid ){
        return next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    };
    let table = this.roomMgr.getTable(stid);
    if(table){
        let res = table.gameExit({
            reason:msg.reason,
            uid:suid
        });
        next(null,res);
    }else
    {
        next(null,{
            code:code.FAIL,
            gameId:code.GAME_ID.YCMJ,
            msg:''
        });
    }
}
